#include <string.h>
#include <glib.h>
#include <gtk/gtk.h>

#include "setting-main/vcd-setting-main.h"
#include "setting-close/vcd-setting-close.h"
#include "setting-network/vcd-setting-network.h"
#include "setting-network/vcd-wire.h"
#include "setting-diagnosis/vcd-diagnosis.h"
#include "setting-other/vcd-setting-other.h"
#include "vcd-setting-window.h"

#include "../../vcd-common-api/vcd-conf/vcd-conf.h"
#include "../../vcd-common-api/vcd-common.h"


SysSetting* g_sys_setting = NULL;

static GtkWidget* create_up_notebook(void);
static GtkWidget* create_setting_window_toplevel(void);
static void change_pic(GtkNotebook *notebook, GtkWidget *page, guint page_num);

static GtkWidget* create_up_notebook(void)
{
    GtkWidget *notebook;

    //create notebook
    notebook = gtk_notebook_new();
    gtk_notebook_set_tab_pos(GTK_NOTEBOOK(notebook), GTK_POS_LEFT);//设置notebook标题栏的位置
    gtk_widget_set_size_request(notebook, 684, 390);//设置小控件的大小
    return notebook;
}

static GtkWidget* create_setting_window_toplevel(void)
{
    GtkWidget* sys_set_window = NULL;
    sys_set_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_modal((GtkWindow *)sys_set_window,     FALSE);
    gtk_window_set_keep_above(GTK_WINDOW(sys_set_window), FALSE);
    gtk_window_set_position(GTK_WINDOW(sys_set_window),   GTK_WIN_POS_CENTER_ALWAYS);
    #ifdef __linux__
    gtk_window_set_decorated(GTK_WINDOW(sys_set_window),  FALSE);
    #else
    gtk_window_set_resizable(GTK_WINDOW(sys_set_window), FALSE);
    #endif
    gtk_container_set_border_width(GTK_CONTAINER(sys_set_window), 0);
    return sys_set_window;
}

//check passwd is ok
int check_system_passwd(const gchar* passwd)
{
    struct vcd_conf* cf = g_vcd_conf;
    M_DEBUG("record sys_passwd=%s, input sys_passwd=%s", cf->sys_passwd, passwd);
    if (strcmp(passwd, "VeryCloud") == 0 || strcmp(passwd, cf->sys_passwd) == 0) {
        return 0;
    }
    M_DEBUG("input sys_passwd error!");
    return -1;
}

static void change_pic(GtkNotebook *notebook, GtkWidget *page, guint page_num)
{
    GtkWidget *page1  = gtk_notebook_get_nth_page(notebook, 0);
    GtkWidget *label1 = gtk_notebook_get_tab_label(notebook, page1);
    set_css(label1, "label{border-radius:0px;color:white}");
    set_img(label1, "label", "system.png");

    GtkWidget *page2  = gtk_notebook_get_nth_page(notebook, 1);
    GtkWidget *label2 = gtk_notebook_get_tab_label(notebook, page2);
    set_css(label2, "label{border-radius:0px;color:white}");
    set_img(label2, "label", "network.png");

    GtkWidget *page3  = gtk_notebook_get_nth_page(notebook, 2);
    GtkWidget *label3 = gtk_notebook_get_tab_label(notebook, page3);
    set_css(label3, "label{border-radius:0px;color:white}");
    set_img(label3, "label", "repair.png");

    GtkWidget *page4  = gtk_notebook_get_nth_page(notebook, 3);
    GtkWidget *label4 = gtk_notebook_get_tab_label(notebook, page4);
    set_css(label4, "label{border-radius:0px;color:white}");
    set_img(label4, "label", "other.png");

    GtkWidget *labelcurrent = gtk_notebook_get_tab_label(notebook, page);
    switch (page_num)
    {
    case 0:
        set_css(labelcurrent, "label{border-radius:0px;color:white}");
        set_img(labelcurrent, "label", "system1.png");
        break;
    case 1:
        set_css(labelcurrent, "label{border-radius:0px;color:white}");
        set_img(labelcurrent, "label", "network1.png");
        set_network_information();
        break;
    case 2:
        set_css(labelcurrent, "label{border-radius:0px;color:white}");
        set_img(labelcurrent, "label", "repair1.png");
        break;
    case 3:
        set_css(labelcurrent, "label{border-radius:0px;color:white}");
        set_img(labelcurrent, "label", "other1.png");
        break;
    }
}


//create window
int create_setting_window(void)
{
    if (g_sys_setting != NULL && g_sys_setting->sys_set_window != NULL) {
        btn_quit_sys_set(g_sys_setting->sys_set_window);
    }

    GtkWidget *notelabel_sys_setting = NULL;
    GtkWidget *notelabel_network_setting = NULL;
    GtkWidget *notelabel_network_diagnosis = NULL;
    GtkWidget *notelabel_other = NULL;

    GtkWidget *notebook_main;
    GtkWidget *notebook_network;
    GtkWidget *grid_diagnosis;
    GtkWidget *notebook_other;

    GtkWidget *notebook, *hboxdown_close;
    GtkWidget *vboxtotal;

    GtkWidget* sys_set_window = NULL;

    if (g_sys_setting == NULL) {
        g_sys_setting = g_malloc(sizeof(SysSetting));
        g_return_val_if_fail(g_sys_setting != NULL, -1);
    }

    //creat windown toplevel
    sys_set_window = create_setting_window_toplevel();

    //create notebook
    notebook = create_up_notebook();

    //new down box
    vboxtotal = gtk_box_new(GTK_ORIENTATION_VERTICAL,   0);

    //new label
    #ifdef __linux__
    notelabel_sys_setting        = gtk_label_new("\n\n\n\n设置");
    notelabel_network_diagnosis  = gtk_label_new("\n\n\n\n诊断");
    notelabel_network_setting    = gtk_label_new("\n\n\n\n网络");
    notelabel_other              = gtk_label_new("\n\n\n\n其他");
    #else
    notelabel_network_setting    = gtk_label_new("网络");
    #endif
    if(notelabel_sys_setting)
        gtk_widget_set_size_request(notelabel_sys_setting,     75, 70);
    if(notelabel_network_setting)
        gtk_widget_set_size_request(notelabel_network_setting, 75, 70);
    if(notelabel_network_diagnosis)
        gtk_widget_set_size_request(notelabel_network_diagnosis,  75, 70);

    //setting main
    if(notelabel_sys_setting){
        notebook_main  = get_setting_main_notebook();
        gtk_notebook_append_page(GTK_NOTEBOOK(notebook), notebook_main,    notelabel_sys_setting);
    }

    //setting network
    if(notelabel_network_setting){
        notebook_network = get_setting_network_notebook();
        gtk_notebook_append_page(GTK_NOTEBOOK(notebook), notebook_network, notelabel_network_setting);
    }

    //diagnosis
    if(notelabel_network_diagnosis){
        grid_diagnosis = get_setting_diagnosis();
        gtk_notebook_append_page(GTK_NOTEBOOK(notebook), grid_diagnosis,   notelabel_network_diagnosis);
    }

    //other
    if(notelabel_other){
        grid_diagnosis = get_setting_diagnosis();
        notebook_other = vcd_setting_other_notebook();
        gtk_notebook_append_page(GTK_NOTEBOOK(notebook), notebook_other,   notelabel_other);
    }


    //close box
    hboxdown_close  = get_setting_close_box(sys_set_window);

    gtk_notebook_set_show_border((GtkNotebook *)notebook, FALSE);

    //pack up and down
    gtk_box_pack_start(GTK_BOX(vboxtotal), notebook, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(vboxtotal), hboxdown_close, FALSE, FALSE, 0);

    gtk_container_add(GTK_CONTAINER(sys_set_window), vboxtotal);

    #ifdef __linux__
    g_signal_connect(G_OBJECT(notebook),         "switch-page", G_CALLBACK(change_pic),         NULL);
    #endif

    //show all
    gtk_widget_show_all(sys_set_window);    

    /*----------------- CSS----------------- */
    GtkCssProvider *provider;
    GdkDisplay *display;
    GdkScreen *screen;
    provider = gtk_css_provider_get_default();
    display = gdk_display_get_default();
    screen = gdk_display_get_default_screen(display);
    gtk_style_context_add_provider_for_screen(screen, GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
    gtk_css_provider_load_from_data(GTK_CSS_PROVIDER(provider), "button.combo{ border:1px solid rgb(233,233,233);"
                                    "border-radius:0px;background:transparent}"
                                    , -1, NULL);

    if(notelabel_sys_setting)
        set_img(notelabel_sys_setting,     "label", "system1.png");
    if(notelabel_network_diagnosis)
        set_img(notelabel_network_diagnosis,  "label", "repair.png");
    if(notelabel_network_setting)
        set_img(notelabel_network_setting, "label", "network.png");

    set_css(notebook, "header{border-radius:0px;background-color:rgb(50,50,50);"
            "border:none}");

    g_sys_setting->sys_set_window = sys_set_window;
    return 0;
}

//hide window
void hide_setting_window(void)
{
    SysSetting* p =  g_sys_setting;
    gtk_widget_hide(p->sys_set_window);
}

//show window
void show_setting_window(void)
{
    SysSetting* p =  g_sys_setting;
    gtk_widget_show_all(p->sys_set_window);
}
